//
//  IFIndexAudioCell.m
//  chanlin
//
//  Created by Ryan on 15/6/20.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "IFIndexAudioCell.h"
#import "BlockUI.h"
#import "UIView+Utils.h"
#import "STKAudioPlayer.h"


@implementation IFIndexAudioView : UIView

- (void)setProgress:(CGFloat)progress{
    _progress=progress;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, self.backgroundColor.CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextFillRect(ctx, rect);
    
    
    if(_progress>0){
    CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextStrokeEllipseInRect(ctx, CGRectInset(self.bounds, -1, -1));
    CGPoint center = CGPointMake(self.bounds.origin.x+self.bounds.size.width/2, self.bounds.origin.y+self.bounds.size.height/2);
    CGContextMoveToPoint(ctx, center.x, center.y);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
            
    CGContextAddArc(ctx, center.x, center.y, (self.bounds.size.width+2)/2-1,  DEGREES_TO_RADIANS(-90), DEGREES_TO_RADIANS(-90+_progress*360), 0);
    CGContextFillPath(ctx);
    }
    
 

}

@end

@implementation IFIndexAudioCell

- (void)uiInit{
    [super uiInit];
    self.titleLabel = [[IFIndexTitleView alloc] initWithFrame:CGRectMake(cellEdgeCap, cellEdgeCap, cellAudioTitleWidth, cellTitleHeight)];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:15.f];
    // self.titleLabel.numberOfLines = 2;
    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
    [self.baseView addSubview:self.titleLabel];
    
    
    self.cellSeperatorImgView=[CALayer layer];
    self.cellSeperatorImgView.frame=CGRectMake(0, 0.f, GlobleWidth, 0.5f);
    self.cellSeperatorImgView.backgroundColor=RGB(0xe0, 0xe0, 0xde).CGColor;
    [self.baseView.layer addSublayer:self.cellSeperatorImgView];
    


    
    self.audioView=[[IFIndexAudioView alloc] init];
    self.audioView.frame=CGRectMake(GlobleWidth-cellEdgeCap-40.f, cellEdgeCap, 42.f, 42.f);
    self.audioView.layer.cornerRadius=21.f;
    self.audioView.backgroundColor=RGB(225, 225, 225);
    self.audioView.clipsToBounds=YES;
    [self.baseView addSubview:self.audioView];
    
    self.play = [[UIImageView alloc] init];
    self.play.userInteractionEnabled=YES;
    self.play.backgroundColor=[UIColor whiteColor];
    self.play.image=[UIImage imageNamed:@"audio_play1.png"];
    self.play.frame=CGRectInset(self.audioView.bounds, 2, 2);
    self.play.layer.cornerRadius=20.f;
    
    [self.audioView addSubview:self.play];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAudio)];
    [self.play addGestureRecognizer:tap];
    
    self.timeLabel=[[UILabel alloc] init];
    self.timeLabel.font=[UIFont systemFontOfSize:11.f];
    self.timeLabel.textColor=RGB(0x56, 0x56, 0x5e);
    self.timeLabel.textAlignment=NSTextAlignmentCenter;
    self.timeLabel.frame=CGRectMake(GlobleWidth-58-cellEdgeCap, self.audioView.bottom+6,70.f, self.timeLabel.font.lineHeight);
    [self.baseView addSubview:self.timeLabel];
    
    
    
}

- (void)loadData:(CLList *)newsList{
    
    [super loadData:newsList];
    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
     self.titleLabel.text = newsList.title;
    self.audioUrl=newsList.audio;
    self.audioId=newsList.itemId;
    if([self.audioId isEqualToString:[IFAudioPlayerManager shareInstance].tag]){
        [self checkStatus];
    }else{
        if([IFAudioPlayerManager shareInstance].delegate2==self){
            [IFAudioPlayerManager shareInstance].delegate2=nil;
        }
        self.play.image=[UIImage imageNamed:@"audio_play1.png"];
        [self.audioView setProgress:0];
    }
    if(_isStrNotNull(newsList.audioTime)){
        self.timeLabel.text=newsList.audioTime;
        self.duartion=newsList.audioTime;
    }else{
        self.timeLabel.text=@"";
        self.duartion=nil;
    }
    
}

- (void)loadCollect:(CLCollect *)newsList{
    
    [super loadCollect:newsList];
    self.titleLabel.text = newsList.title;
    self.audioUrl=newsList.audio;
    self.audioId=newsList.itemId;
    if([self.audioId isEqualToString:[IFAudioPlayerManager shareInstance].tag]){
        [self checkStatus];
    }else{
        if([IFAudioPlayerManager shareInstance].delegate2==self){
            [IFAudioPlayerManager shareInstance].delegate2=nil;
        }
        self.play.image=[UIImage imageNamed:@"audio_play1.png"];
        [self.audioView setProgress:0];
    }
    if(_isStrNotNull(newsList.audioTime)){
        self.timeLabel.text=newsList.audioTime;
        self.duartion=newsList.audioTime;
    }else{
        self.timeLabel.text=@"";
        self.duartion=nil;
    }

    
}

- (void)playAudio{
    IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
    
    NSString *tag=self.audioId;
    if(![tag isEqualToString:[IFAudioPlayerManager shareInstance].tag]){
        [[IFAudioPlayerManager shareInstance] stop];
    }

    
    if(![ap isPlaying]){
  
    
        [IFAudioPlayerManager shareInstance].tag=tag;
        [IFAudioPlayerManager shareInstance].delegate2=self;
        
        [[IFAudioPlayerManager shareInstance] playWithURLString:self.audioUrl title:self.titleLabel.text];
        self.play.image=[UIImage imageNamed:@"audio_play0.png"];
        self.titleLabel.textColor=[UIColor redColor];
        
    }else if([ap isPause]){
        [ap resume];
        
        self.play.image=[UIImage imageNamed:@"audio_play0.png"];
       self.titleLabel.textColor=[UIColor redColor];
        
    }else if([ap isRunning]) {
        [ap pause];
        self.play.image=[UIImage imageNamed:@"audio_paly2.png"];
        self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
        


        
        
    }else{

    }

    
    
   

}

//- (void)willpause:(NSString *)tag{
//    self.play.image=[UIImage imageNamed:@"audio_paly2.png"];
//    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
//
//}

- (void)playingStstusTotalTime:(double) total current:(double)current{
    self.timeLabel.text=[NSString stringWithFormat:@"%@/%@",
                    [[IFAudioPlayerManager shareInstance] formatSecond:current],
                    [[IFAudioPlayerManager shareInstance] formatSecond:total]];
}

- (void)audioProgerss:(float)progerss{
    double total=[IFAudioPlayerManager shareInstance].player.duration;
    self.timeLabel.text=[NSString stringWithFormat:@"%@/%@",
                         [[IFAudioPlayerManager shareInstance] formatSecond:progerss],
                         [[IFAudioPlayerManager shareInstance] formatSecond:total]];
    [self.audioView setProgress:progerss/total];
}

- (void)finishwithReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration{
    
    if(self.duartion){
        self.timeLabel.text=self.duartion;
    }else{
        self.timeLabel.text=@"";
    }
    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
    self.play.image=[UIImage imageNamed:@"audio_play1.png"];
    [self.audioView setProgress:0];
    [[IFAudioPlayerManager shareInstance] stop];
   
    

}

- (NSString *)getAudioTitle{
    return self.timeLabel.text;
}

- (void)checkStatus{
 
    
    IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
    double current=[IFAudioPlayerManager shareInstance].player.progress;
    double total=[IFAudioPlayerManager shareInstance].player.duration;
    self.timeLabel.text=[NSString stringWithFormat:@"%@/%@",
                         [[IFAudioPlayerManager shareInstance] formatSecond:current],
                         [[IFAudioPlayerManager shareInstance] formatSecond:total]];

    self.titleLabel.textColor=[UIColor redColor];
    ap.delegate2=self;
    
    
    if(![ap isPause]){
        
        self.play.image=[UIImage imageNamed:@"audio_play0.png"];
        
    }else{
        
        self.play.image=[UIImage imageNamed:@"audio_play0.png"];
    }
    
}

- (void)willStop:(NSString *)tag{
    if([IFAudioPlayerManager shareInstance].delegate2==self){
        [IFAudioPlayerManager shareInstance].delegate2=nil;
    }
    self.play.image=[UIImage imageNamed:@"audio_play1.png"];
    [self.audioView setProgress:0];
    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
    if(self.duartion){
        self.timeLabel.text=self.duartion;
    }else{
        self.timeLabel.text=@"";
    }

}



@end
